﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {
    /// <summary>
    /// 微博客户端
    /// </summary>
    public partial class OClient2 {
        /// <summary>
        /// 采用OAuth授权方式为必填参数，其他授权方式不需要此参数，OAuth授权后获得。
        /// </summary>
        private string access_token = null;

        /// <summary>
        /// 采用OAuth授权方式不需要此参数，其他授权方式为必填参数，数值为应用的AppKey。
        /// </summary>
        private string source = null;

        /// <summary>
        /// 表示字符编码。默认 UTF-8 。
        /// </summary>
        private Encoding encoding = Encoding.UTF8;

        /// <summary>
        /// 初始化微博客户端
        /// </summary>
        /// <param name="access_token">采用OAuth授权方式为必填参数，其他授权方式不需要此参数，OAuth授权后获得。</param>
        /// <param name="source">采用OAuth授权方式不需要此参数，其他授权方式为必填参数，数值为应用的AppKey。</param>
        /// <param name="encoding">表示字符编码。默认 UTF-8 。</param>
        public OClient2(string access_token = null, string source = null, Encoding encoding = null) {
            if (!String.IsNullOrEmpty(access_token)) {
                this.access_token = access_token;
            } else if (!String.IsNullOrEmpty(source)) {
                this.source = source;
            } else {
                throw new WeiboException("You fill out the access_token and at least one item in the source.");
            }

            if (encoding != null) {
                this.encoding = encoding;
            }
        }

        /// <summary>
        /// 添加 ACCESS_TOKEN 或 APP_KEY
        /// </summary>
        /// <param name="data"></param>
        /// <returns>成功返回 TRUE，否则返回 FALSE</returns>
        /// <exception cref="WeiboException"></exception>
        public bool SetData(ref Dictionary<string, object> data){
            if (!String.IsNullOrEmpty(access_token)) {
                data.Add("access_token", access_token);
            } else if (!String.IsNullOrEmpty(source)) {
                data.Add("source", source);
            } else {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 微博 GET 方法
        /// </summary>
        /// <param name="url">API URL</param>
        /// <param name="data">请求参数</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string Get(string url, Dictionary<string, object> data) {
            string response = string.Empty;

            if (SetData(ref data) == false) {
                throw new WeiboException("ACCESS_TOKEN 和 APP_KEY 不能为空！");
            }

            try {
                byte[] temp = Tools.Get(url, data, encoding);
                response = encoding.GetString(temp);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 微博 GET 方法
        /// </summary>
        /// <param name="url">API URL</param>
        /// <param name="data">请求参数</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string Get(string url, string data) {
            string response = string.Empty;

            try {
                byte[] temp = Tools.Get(url, data, encoding);
                response = encoding.GetString(temp);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 微博 POST 方法
        /// </summary>
        /// <param name="url">API URL</param>
        /// <param name="data">请求参数</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string Post(string url, Dictionary<string, object> data) {
            string response = string.Empty;

            if (SetData(ref data) == false) {
                throw new WeiboException("ACCESS_TOKEN 和 APP_KEY 不能为空！");
            }

            try {
                byte[] temp = Tools.Post(url, data, encoding);
                response = encoding.GetString(temp);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 微博 POST 方法
        /// </summary>
        /// <param name="url">API URL</param>
        /// <param name="data">请求参数</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string Post(string url, string data) {
            string response = string.Empty;

            try {
                byte[] temp = Tools.Post(url, data, encoding);
                response = encoding.GetString(temp);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
